package com.ruoyi.project.erp.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.erp.domain.WorklogTemp;
import com.ruoyi.project.erp.dto.AssignWorklogDTO;
import com.ruoyi.project.erp.dto.WorklogSubmitDTO;
import com.ruoyi.project.erp.service.IProjectService;
import com.ruoyi.project.erp.service.ITaskService;
import com.ruoyi.project.erp.service.IWorklogTempService;
import com.ruoyi.project.erp.vo.DailyWorklogSummaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/erp/worklog/temp")
public class WorklogTempController extends BaseController {

    @Autowired
    private IWorklogTempService worklogTempService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ITaskService taskService;
    /**
     * 获取临时工时详细信息，用于修改时的数据回显
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(worklogTempService.getTempWorklogById(id));
    }
    /**
     * 获取指定日期的工时汇总信息
     */
    @GetMapping("/summary")
    public AjaxResult getDailySummary(@RequestParam("workDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date workDate) {
        logger.debug("开始执行查看暂存工时！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        DailyWorklogSummaryVO summary = worklogTempService.getDailySummary(workDate);
        return AjaxResult.success(summary);
    }
    /**
     * 新增或修改暂存工时
     * - 不带id为新增
     * - 带id为修改
     */
    @Log(title = "工时暂存", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody WorklogTemp worklogTemp) {
        return worklogTempService.saveOrUpdateTempWorklog(worklogTemp);
    }



    @Log(title = "提交当日工时", businessType = BusinessType.INSERT)
    @PostMapping("/submitByDate")
    public AjaxResult submitByDate(@RequestBody WorklogSubmitDTO submitDto) {
        // 从 DTO 中获取日期
        Date workDate = submitDto.getWorkDate();
        if (workDate == null) {
            return AjaxResult.error("工作日期不能为空");
        }

        // 调用 Service 层执行核心业务逻辑
        return worklogTempService.submitTempWorklogsByDate(workDate);
    }


    /*
    我参加的项目列表
     */
    @GetMapping("/getMyProject")
    public AjaxResult getMyProject(@RequestParam("date") Date date) {
        return AjaxResult.success(projectService.getMyProjectByDate(date));
    }

    /*
我参加的项目列表
 */
    @GetMapping("/getMyTask")
    public AjaxResult getMyTask(@RequestParam("projectId") String projectId) {
        return AjaxResult.success(taskService.getMyTaskByProject(projectId));
    }


    @Log(title = "删除工时暂存", businessType = BusinessType.DELETE)
    @GetMapping("/deleteWorklogTemp")
    public AjaxResult remove(@RequestParam("id") Long id) {
        return toAjax(worklogTempService.deleteWorklogTempById(id));
    }
}
